package com.ruoyi.material.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateDevice.domain.MateDevice;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.material.domain.MateProductDevice;
import com.ruoyi.material.domain.MateProductModel;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.mapper.MateManufactuterMapper;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.material.service.MateManufactuterService;
import com.ruoyi.material.service.MateProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/materialproduct")
public class MaterialProductController extends BaseController {

    @Autowired
    MateProductService mateProductService;

    @Autowired
    MateProductMapper mateProductMapper;

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    MateManufactuterMapper mateManufactuterMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Autowired
    MateDeviceMapper mateDeviceMapper;

    /**
     * 新增保存产品
     */
    @RequiresPermissions("business:product:add")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody MaterialProduct materialProduct) {
        materialProduct.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        int a = mateProductService.insertMateProduct(materialProduct);
        if (a == 1) {
            return AjaxResult.success("添加成功");

        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @GetMapping("/selectDevice")
    public List<MateDevice> selectDevice() {
        List<MateDevice> list = mateDeviceMapper.selectDevice(SecurityUtils.getDeptId());
        return list;
    }


    /**
     * 查询原材料列表
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "mateProductName", required = false) String mateProductName) {
        startPage();
        MaterialProduct materialProduct = new MaterialProduct();
        materialProduct.setMateProductName(mateProductName);
        materialProduct.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        List<MaterialProduct> list = mateProductService.selectMateriProduct(materialProduct);
        return getDataTable(list);
    }

    @GetMapping("/selectInformation")
    public List<MaterialProduct> selectInformation(@RequestParam(value = "mateproductId", required = false) Long mateproductId) {
        List<MaterialProduct> list = mateProductMapper.selectMateriProductById(mateproductId);
        List<MateProductDevice> mateProductDevices = mateProductMapper.selectMateProductDevice(mateproductId);
        for (MaterialProduct materialProduct : list) {
            materialProduct.setMateProductDevices(mateProductDevices);
        }
        return list;
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult updateMateProduct(@RequestBody MaterialProduct materialProduct) {
        int a = mateProductService.updateMateProduct(materialProduct);
        if (a == 1) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @GetMapping("/selectMatemanufcatuter")
    public List<MaterialManufactuter> selectManufacturer() {
        MaterialManufactuter materialManufactuter = new MaterialManufactuter();
        materialManufactuter.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        List<MaterialManufactuter> list = mateManufactuterService.selectMateManufactuter(materialManufactuter);
        return list;
    }

    @GetMapping("/remove/{mateproductId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("mateproductId") Long mateproductId) {
        mateProductMapper.deleteMaterialProduct(mateproductId);
        mateProductMapper.deleteMaterialProductModelById(mateproductId);
        mateProductMapper.deleteMaterialProductDevice(mateproductId);
        return AjaxResult.success("删除成功");
    }

    @PostMapping("/updateModelInfo")
    public AjaxResult updateModelInfo(@RequestBody MateProductModel mateProductModel) {
        return AjaxResult.success(mateProductService.updateModelInfo(mateProductModel));
    }
}
